<?php
if (!defined('APP') or !constant('APP')) die('Direct access not allowed!');

class DemotCategoryModel
{    
    private static function exist($name)
    {
        $query = "SELECT id FROM `demots_categories` WHERE name = :name;";
        $result = DB::getPDO()->prepare($query);
        $result->execute(array(
            ':name' => trim($name)
            ));  
        $result = $result->fetchAll();
        
        if (sizeof($result) < 1) { return false; }
        
        return true;   
    }
    
    public static function update($request)
    {   
        $data = array();
        $data[':id'] = isset($request['id']) ? intval($request['id']) : 0;
        
        if ($data[':id']==0) return false;
        
        $data[':name'] = isset($request['name']) ? $request['name'] : "Bład w nazwie";
        $data[':img'] = isset($request['img']) ? $request['img'] : "";
        
        if (empty($data[':name']))                       
        {    
            flash_message::setMessage("Nie prawidłowe dane. Podana nazwa jest pusta albo istnieje już bazie danych.!");
            System::redirect("index.php?controller=demot&action=show_categories");
            return false; 
        }
        
             
        $query = "UPDATE `demots_categories` SET `name`=:name, `img`=:img WHERE `id`=:id";   
            
        $result = DB::getPDO()->prepare($query);
        
        $result->execute($data);            
    }
    
    public static function save($request)
    {
        $data = array();
       
        $data[':name'] = isset($request['name']) ? $request['name'] : "";
        $data[':img'] = isset($request['img']) ? $request['img'] : "";
       
        if (empty($data[':name']) || self::exist($data[':name']))                       
        {    
            flash_message::setMessage("Nie prawidłowe dane. Podana nazwa jest pusta albo istnieje już bazie danych.!");
            System::redirect("index.php?controller=demot&action=show_categories");
            return false; 
        }
                        
        $query = "INSERT INTO `demots_categories` (`name`,`img`) VALUES (:name, :img);";
        
        $result = DB::getPDO()->prepare($query);
        return $result->execute($data);    
    }
    
      public static function delete($id)
    {        
        $query = "SELECT id FROM `demots` WHERE `id_category` = :id;";
        $result = DB::getPDO()->prepare($query);
        $result->execute(array(
            ':id' => intval($id)
            ));  
        $result = $result->fetchAll();
        
        if (sizeof($result) > 0) { 
            return false;             
        }   
       
        $data = array(':id' => intval($id));
       
        $query = "DELETE FROM `demots_categories` WHERE `id` = :id";
        $result = DB::getPDO()->prepare($query);
        $result->execute($data);  
    
        return $result;
    }
    
    public static function fetch($id)
    {
        $data = array(":id" => intval($id));
        $query = "SELECT * FROM `demots_categories` WHERE id = :id";
        $result = DB::getPDO()->prepare($query);
        $result->execute($data);  
        $result = $result->fetch();
        return $result;
    }
    
    public static function fetchAll()
    {
        $query = "SELECT * FROM `demots_categories` ORDER by name";
        
        $result = DB::getPDO()->prepare($query);
        $result->execute();  
        
        $result = $result->fetchAll();        
        return $result;
    }
}